package com.gxa.autoparts.admin.service.impl;

import com.gxa.autoparts.admin.service.AdminRoleService;
import com.gxa.autoparts.admin.service.AdminRoleVoService;
import com.gxa.autoparts.admin.service.AdminService;
import com.gxa.autoparts.admin.service.RoleService;
import com.gxa.autoparts.admin.vo.AdminRoleVo;
import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.Admin;
import com.gxa.autoparts.core.pojo.AdminRole;
import com.gxa.autoparts.core.pojo.Role;
import com.gxa.autoparts.core.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Service("adminRoleVoService")
public class AdminRoleVoServiceImpl implements AdminRoleVoService {
    @Autowired
    private AdminService adminService;
    @Resource
    private AdminRoleService adminRoleService;
    @Resource
    private RoleService roleService;
    @Override
    public void addAdmin(AdminRoleVo adminRoleVo) throws BusinessException {
        Admin admin = new Admin();
        admin.setAccount(adminRoleVo.getAccount());
        admin.setMail(adminRoleVo.getMail());
        admin.setState(adminRoleVo.getState());
        admin.setRegTime(new Timestamp(System.currentTimeMillis()));
        admin.setPwd(adminRoleVo.getPwd());
        admin.setPhone(adminRoleVo.getPhone());
        admin.setName(adminRoleVo.getName());
        Admin insert = adminService.insert(admin);

        AdminRole adminRole = new AdminRole();
        List<Role> select = roleService.selectByDesc(adminRoleVo.getDesc());
        if (select.size()!=1){
            throw new BusinessException(ExceptionCode.WRONG_ROLE_DESC);
        }
        adminRole.setAid(insert.getId());
        adminRole.setRid(select.get(0).getId());
        adminRoleService.insert(adminRole);
    }

    @Override
    public void updateAdmin(AdminRoleVo adminRoleVo) throws BusinessException {
        Admin admin = new Admin();
        admin.setId(adminRoleVo.getId());
        admin.setAccount(adminRoleVo.getAccount());
        admin.setMail(adminRoleVo.getMail());
        admin.setState(adminRoleVo.getState());
        admin.setRegTime(new Timestamp(System.currentTimeMillis()));
        admin.setPwd(adminRoleVo.getPwd());
        admin.setPhone(adminRoleVo.getPhone());
        admin.setName(adminRoleVo.getName());
        adminService.updateById(admin);

        AdminRole adminRole = new AdminRole();
        List<Role> select = roleService.selectByDesc(adminRoleVo.getDesc());
        if (select.size()!=1){
            throw new BusinessException(ExceptionCode.WRONG_ROLE_DESC);
        }
        adminRole.setAid(admin.getId());
        adminRole.setRid(select.get(0).getId());
        List<AdminRole> select1 = adminRoleService.select(adminRole);
        if (select1.size()!=0){
            throw new BusinessException(ExceptionCode.WRONG_ROLE_ADMIN_ID);
        }
        adminRole.setId(select.get(0).getId());
        adminRoleService.updateById(adminRole);
    }
}
